/**
  ************************************* Copyright ******************************
  * FileName   : debug.c
  * Version    : v1.0
  * Author     : yychao
  * Date       : 2023-03-03
  * Description:
  * Function List:
  	1. ....
  	   <version>:
  <modify staff>:
  		  <data>:
   <description>:
  	2. ...
  ******************************************************************************
 */
#include "cJSON.h"
#include "log.h"
#include "debug.h"
#include "usart.h"
#include "string.h"
#include "adc_app.h"
#include "ds18b20.h"
#include "dma.h"
#include "lfs_port.h"
#include "gpio.h"
#include "soft_timer.h"
#include "rtc.h"
#include "io_ctrl.h"
#include "my_protocol.h"
/**********************************************************/



/**********************************************************/
void debug_deal(usart_cache* recv) //DataBuff* recv
{

    cJSON* json;
    cJSON* pwm_value;
    cJSON* coil_value;
    cJSON* input_value;
    cJSON* read;
    cJSON* write;
    cJSON* value;
    uint16_t array_num;

    uint16_t pause_value = 0;

    const char* ptr = NULL;
    ptr = (const char*)&recv->data[3];
    json = cJSON_Parse(ptr);
    if (json == NULL) {
        LOG_DEBUG("json fmt error: \r\n %s\r\n", cJSON_GetErrorPtr());
        cJSON_Delete(json);
        return;
    }
//    LOG_DEBUG("json fmt ok\r\n");

    CONSOLE_CLEAR();
    LOG_KEY("This Is Json File Response:\r\n");
    pwm_value = cJSON_GetObjectItem(json, "PWM_VALUE");
    if (pwm_value != NULL) {
        write = cJSON_GetObjectItem(pwm_value, "W");
        if (write != NULL) {
            array_num = cJSON_GetArraySize(write);
            for(int cnt = 0; cnt < array_num; cnt++) {
                value = cJSON_GetArrayItem(write, cnt);
                if(value == NULL) {
                    continue;
                }
                switch(cnt) {
                case 0:
                    LOG_DEBUG("PA11 PWM1_VALUE: %d \r\n", value->valueint);
                    if((value->valueint < 0)||(value->valueint > 100)) {
                        LOG_DEBUG("PA11 PWM1_VALUE Is Overflow \r\n");
                    } else {
                        //duty_cycle = PWM1_VALUE/100;
                        //duty_cycle = Pulse/Period*100%;
                        //Pulse = Period*duty_cycle;
                        //pause_value = 100*value->valuedouble/100;

                        pause_value = value->valueint;
                        io_ctrl_st.pwm1 = pause_value;
//                        TIM_SetCmp4(TIM1, pause_value);
                    }
                    break;
                case 1:
                    LOG_DEBUG("PB14 PWM2_VALUE: %d \r\n", value->valueint);
                    if((value->valueint < 0)||(value->valueint > 100)) {
                        LOG_DEBUG("PB14 PWM2_VALUE Is Overflow \r\n");
                    } else {
                        pause_value = value->valueint;
                        io_ctrl_st.pwm2 = pause_value;
//                        TIM_SetCmp2(TIM1, pause_value);
                    }
                    break;
                }
            }
        }
        read = cJSON_GetObjectItem(pwm_value, "R");
        if (read != NULL) {
            LOG_DEBUG("PA11 PWM1_VALUE: %d \r\n", io_ctrl_st.pwm1);
            LOG_DEBUG("PB14 PWM2_VALUE: %d \r\n", io_ctrl_st.pwm2);
        }
    }

    char* coil_str[] = {
        "BEEP",
        "FAN1",
        "FAN2",
        "FAN3",
        "RED1",
        "RED3",
        "BLUE1",
        "BLUE3",
        "YELLOW1",
        "YELLOW2",
    };
    coil_value = cJSON_GetObjectItem(json, "COIL_VALUE");
    if (coil_value != NULL) {
        write = cJSON_GetObjectItem(coil_value, "W");
        if (write != NULL) {
            array_num = cJSON_GetArraySize(write);
            for(int cnt = 0; cnt < array_num; cnt++) {
                value = cJSON_GetArrayItem(write, cnt);
                if(value == NULL) {
                    continue;
                }
                LOG_DEBUG("COIL[%d]:%s %s \r\n", cnt, coil_str[cnt], value->valuestring);
                if(0 == strcmp("ON", value->valuestring)) {
                    pro_data_trans_st.coil[cnt] = 0x55;
                } else if(0 == strcmp("OFF", value->valuestring)) {
                    pro_data_trans_st.coil[cnt] = 0x00;
                }
            }
        }
        read = cJSON_GetObjectItem(coil_value, "R");
        if (read != NULL) {
            for(uint8_t i = 0; i < 10; i++) {
                LOG_DEBUG("COIL[%d] %s:", i, coil_str[i]);
                if(pro_data_recv_st.coil[i] == 0x55) {
                    LOG_DEBUG("ON ");
                } else {
                    LOG_DEBUG("OFF ");
                }
                LOG_DEBUG("\r\n")
            }

        }
    }

    input_value = cJSON_GetObjectItem(json, "INPUT_VALUE");
    if (input_value != NULL) {
        read = cJSON_GetObjectItem(input_value, "R");
        if (read != NULL) {
            LOG_DEBUG("INPUT:");
            for(uint8_t i = 0; i < 4; i++) {
                LOG_DEBUG("[%d]", pro_data_recv_st.input[i]);
            }
            LOG_DEBUG("\r\n")
        }
    }

    cJSON_Delete(json);



}


